from neomodel import db

from users.models import UserNode


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回参数
    """
    return {
        'token': token,
        'user': user.id,
        'username': user.username
    }


@db.transaction
def get_or_create(user_id: int, username: str, avatar: str) -> UserNode:
    try:
        n = UserNode.nodes.get(
            user_id=user_id,
            username=username,
            avatar=avatar
        )
    except UserNode.DoesNotExist:
        n = UserNode(
            user_id=user_id,
            username=username,
            avatar=avatar
        ).save()
    return n
